﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TuxedoLib;

namespace SpaceSquirrel
{
	public class OpeningScene : GameSceneBase
	{
		private static readonly int STATIC_WIDTH = 150;

		private string mode;
		private int fade;
		private int modeCounter;

		private Image staticImage;
		private Image scrolly;
		private Image dark;

		private static readonly string[] CH3 = new string[] {
			" XXX  X  X   XXX ",
			"XX XX X  X  X   X",
			"X     X  X      X",
			"X     XXXX    XX ",
			"X     X  X      X",
			"XX XX X  X  X   X",
			" XXX  X  X   XXX "
		};

		private static readonly string[] LOGO = new string[] {
			"XXXXX XXXXX  XXX  X     X       X   X XXXX    XXXX  XXXX  XXXXX  XXX  XXXXX X   X XXXXX  XXX ",
			"  X   X     X   X XX   XX       XX  X X   X   X   X X   X X     X   X X     XX  X   X   X   X",
			"  X   X     X   X X X X X       X X X X   X   X   X X   X X     X     X     X X X   X   X    ",
			"  X   XXX   XXXXX X  X  X       X  XX XXXX    XXXX  XXXX  XXX    XXX  XXX   X  XX   X    XXX ",
			"  X   X     X   X X     X       X   X X       X     X X   X         X X     X   X   X       X",
			"  X   X     X   X X     X       X   X X       X     X  X  X     X   X X     X   X   X   X   X",
			"  X   XXXXX X   X X     X       X   X X       X     X   X XXXXX  XXX  XXXXX X   X   X    XXX ",
		};

		public OpeningScene() : base()
		{
			if (Constants.GO_TO_TITLE)
			{
				this.Next = new TitleScene();
				this.mode = null;
				this.Flags = "";
				this.fade = 0;
				this.modeCounter = 0;
			}
			else
			{
				this.mode = "open";
				this.modeCounter = 0;
				this.Flags = "W";
				this.staticImage = new Image(STATIC_WIDTH, STATIC_WIDTH);
				this.scrolly = new Image(STATIC_WIDTH, 5);
				this.dark = new Image(Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
				this.fade = 0;
				int black = Draw.CreateColor(0, 0, 0);
				int white = Draw.CreateColor(255, 255, 255);
				int[] colors = new int[] { black, white };
				colors = Util.MultiplyList(colors, STATIC_WIDTH * STATIC_WIDTH / 2 + 1);
				Util.ShuffleList(colors);
				int i = 0;
				for (int y = 0; y < STATIC_WIDTH; ++y)
				{
					for (int x = 0; x < STATIC_WIDTH; ++x)
					{
						Draw.SetPixel(this.staticImage, colors[i], x, y);
					}
				}
			}
		}

		public override void ProcessInput(List<MyEvent> events, Dictionary<string, bool> pressedActions)
		{
			foreach (MyEvent e in events)
			{
				if (this.mode == "open")
				{
					if (e.Down && (e.Action == "start" || e.Action == "A" || e.Action == "B"))
					{
						this.mode = "starting";
						this.modeCounter = 0;
					}
				}
			}
		}

		public override void Update()
		{
			this.fade++;
			this.modeCounter++;
			if (this.mode == "starting" && this.modeCounter > 20)
			{
				this.Next = new TitleScene();
			}
			if (this.mode == "open")
			{
				if (this.modeCounter > 200)
				{
					this.mode = "starting";
					this.modeCounter = 0;
				}
			}
		}

		public override void Render(TuxedoLib.Image screen, int renderCounter)
		{
			if (this.mode == "starting")
			{
				if (this.modeCounter < 10)
				{
					screen.Fill(255, 80, 255);
				}
				else
				{
					screen.Fill(200, 200, 200);
				}
			}
			else if (this.mode == "open")
			{
				int left = -Util.RandomInt(0, STATIC_WIDTH);
				int top = -Util.RandomInt(0, STATIC_WIDTH);
				for (int y = top; y < Constants.SCREEN_HEIGHT; y += STATIC_WIDTH)
				{
					for (int x = left; x < Constants.SCREEN_WIDTH; x += STATIC_WIDTH)
					{
						screen.Blit(this.staticImage, x, y);
					}
				}

				this.scrolly.Blit(screen, 0, 0);

				for (int i = 0; i < 5; ++i)
				{
					int y = (renderCounter * 2 + 10 * i + 300) % Constants.SCREEN_HEIGHT;
					screen.Blit(this.scrolly, 0, y);
				}

				int width = CH3[0].Length;
				int height = CH3.Length;
				int limeGreen = Draw.CreateColor(0, 255, 0);
				for (int y = 0; y < height; ++y) {
					for (int x = 0; x < width; ++x){
						if (CH3[y][x] != ' ') {
							Draw.RectangleFill(screen, limeGreen, 20 + x * 8, 20 + y * 8, 8, 8);
						}
					}
				}

				width = LOGO[0].Length;
				height = LOGO.Length;
				for (int y = 0; y < height; ++y) {
					for (int x = 0; x < width; ++x){
						if (LOGO[y][x] != ' ') {
							Draw.RectangleFill(screen, limeGreen, 20 + x * 8, 250 + y * 8, 8, 8);
						}
					}
				}

				int opacity = (int)(this.fade * 1.5);
				if (opacity > 255) opacity = 255;
				
				int v = 255 - opacity;
				if (v > 0)
				{
					this.dark.Fill(0, 0, 0, (byte)v);
					screen.Blit(this.dark, 0, 0);
				}
			}
		}
	}
}
